﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class Player
{
    public  int Point;
    public  int Id;//1为红方，2为蓝方
    public  int Movement;
    public  bool Movable;
    public List<Chess> myChess;
    public int CurChoose;
    public int MineCount;
    public PlayerState playerState;
    public List<Vector2Int> HighLightCells;

    public Player()
    {
        Point = 0;
        Movement = 0;
        Movable = false;
        myChess = new List<Chess>();
        HighLightCells = new List<Vector2Int>();
        playerState = new PlayerState();
        playerState = PlayerState.Staying;
    }
    public void StartTurn()
    {
        Movement = 2;
        Movable = true;
        foreach (var chess in myChess)
        {
            chess.OriPos = chess.Pos;
        }
        Reveal();
        StartMoving();
    }
    
    public void StopMoving()
    {
        //Debug.Log("停止所有棋子移动");
        Movable = false;
        foreach (var chess in myChess)
        {
            //Debug.Log(chess.chessObj.GetComponent<Button>() == null);
            chess.chessObj.GetComponent<Button>().interactable = false;

        }
    }
    public void StartMoving()
    {
        Map.Instance.transform.parent. Find("PlayerUI").GetComponent<PlayerUI>().Refresh();
        Debug.Log("InStartMoving");
        foreach (var chess in myChess)
        {
            
            chess.chessObj.GetComponent<Button>().interactable = true;
            Map.Instance.cellDIC[chess.Pos].cellState = CellState.Chess;
            //Debug.Log(chess.Pos +" " +(Map.Instance.cellDIC[chess.Pos].cellState));
        }
    }
    public void OnclickCells(Vector2Int CellPos)
    {
        if (playerState == PlayerState.Moving)
        {
            if(myChess[CurChoose - 1].Reachable(CellPos))
            {
                if (Map.Instance.cellDIC[CellPos].cellState == CellState.Null)
                {
                    myChess[CurChoose - 1].MoveTo(CellPos);
                    if (Map.Instance.cellDIC[CellPos].cellType==CellType.Blue&&Id==1&& !myChess[CurChoose - 1].Arrive)
                    {
                        AudioManager.Instance.PlayEffectAudio("Arrive", 0.5f);
                        Point += 150;
                        myChess[CurChoose - 1].Arrive = true;
                    }
                    if (Map.Instance.cellDIC[CellPos].cellType == CellType.Red && Id == 2 && !myChess[CurChoose - 1].Arrive)
                    {
                        AudioManager.Instance.PlayEffectAudio("Arrive", 0.5f);
                        Point += 150;
                        myChess[CurChoose - 1].Arrive = true;
                    }
                    if(Map.Instance.cellDIC[CellPos].cellType==CellType.Black&& myChess[CurChoose - 1].Arrive)
                    {
                        Point -= 150;
                        myChess[CurChoose - 1].Arrive = false;
                    }
                }
                else
                {
                    Map.Instance.cellDIC[CellPos].cellState = CellState.Null;
                    myChess[CurChoose - 1].Explode(CellPos);
                }
                EndAction();
            }
        }
        if (playerState == PlayerState.Mining&&Map.Instance.cellDIC[CellPos].cellState!=CellState.Chess&&Vector2.Distance(CellPos,myChess[CurChoose-1].Pos)<3)
        {
            StopMoving();
            MineCount++;
            myChess[CurChoose - 1].SetMine(CellPos);
            if (MineCount >= 2)
            {
                Movable = true;
                StartMoving();
                EndAction();
            }
        }
        if (playerState == PlayerState.Defusing && Map.Instance.cellDIC[CellPos].cellState != CellState.Chess && Vector2.Distance(CellPos, myChess[CurChoose - 1].Pos) < 3)
        {
            myChess[CurChoose - 1].Defuse(CellPos);
            EndAction();
        }
    }

    public void OnclickChess(int chessId)
    {
        if (playerState == PlayerState.Staying || playerState == PlayerState.HaveChooseChess)
        {
            playerState = PlayerState.HaveChooseChess;
            CurChoose = chessId;

        }
    } 
    public void EndAction()
    {
        ClearHighLight();
        Movement--;
        playerState = PlayerState.Staying;
        Map.Instance.transform.parent.Find("PlayerUI").GetComponent<PlayerUI>().Refresh();
        if (Movement <= 0)
        {
            ClearReveal();
            TurnController.Instance.EndTurn();
        }
    }
    public void ClearHighLight()
    {
        foreach (var HLPos in HighLightCells)
        {
            Map.Instance.EndHightLigthCell(HLPos);
        }
        HighLightCells.Clear();
        Map.Instance.Refresh();
    }

    public void ClearReveal()
    {
        foreach(var ce in myChess)
        {
            if (ce.state == ChessState.Watching)
            {
                ce.UnRavel();
            }
        }
    }
    public void Reveal()
    {
        foreach (var ce in myChess)
        {
            if (ce.state == ChessState.Watching)
            {
                ce.Reveal();
            }
        }
    }
}

public enum PlayerState
{
    Staying,
    HaveChooseChess,
    Mining,
    Moving,
    Defusing
}